﻿

Namespace YahooManaged.Finance.Indicators

    ''' <summary>
    ''' Envelopes Indicator
    ''' </summary>
    ''' <remarks></remarks>
    Public Class Env
        Inherits MA

        Public Overrides ReadOnly Property Name() As String
            Get
                Return "Envelopes"
            End Get
        End Property

        Private mIsBufferFixed As Boolean = False
        Public Property IsBufferFixed() As Boolean
            Get
                Return mIsBufferFixed
            End Get
            Set(ByVal value As Boolean)
                mIsBufferFixed = value
            End Set
        End Property
        Private mPercentBuffer As Double = 0.05
        Public Property PercentBuffer() As Double
            Get
                Return mPercentBuffer
            End Get
            Set(ByVal value As Double)
                mPercentBuffer = value
            End Set
        End Property
        Private mFixedBuffer As Double = 1
        Public Property FixedBuffer() As Double
            Get
                Return mFixedBuffer
            End Get
            Set(ByVal value As Double)
                mFixedBuffer = value
            End Set
        End Property


        Public Overrides Function Calculate(ByVal values As IEnumerable(Of KeyValuePair(Of Date, Double))) As Dictionary(Of Date, Double)()
            Dim envResultUpper As New Dictionary(Of Date, Double)
            Dim envResultLower As New Dictionary(Of Date, Double)

            Dim baseResults = MyBase.Calculate(values)
            Dim maResult As Dictionary(Of Date, Double) = baseResults(0)
            Dim histQuotes As New List(Of KeyValuePair(Of Date, Double))(baseResults(1))

            If Me.IsBufferFixed Then
                For Each maRes In maResult
                    envResultUpper.Add(maRes.Key, maRes.Value + Me.FixedBuffer)
                    envResultLower.Add(maRes.Key, maRes.Value - Me.FixedBuffer)
                Next
            Else
                For Each maRes In maResult
                    envResultUpper.Add(maRes.Key, maRes.Value * (1 + Me.PercentBuffer))
                    envResultLower.Add(maRes.Key, maRes.Value * (1 - Me.PercentBuffer))
                Next
            End If

            Return New Dictionary(Of Date, Double)() {envResultUpper, envResultLower, maResult, baseResults(1)}
        End Function

        Public Overrides Function ToString() As String
            Return Me.Name & " " & Me.Period
        End Function

    End Class


End Namespace